load("/Users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Data/CandidateCluster/AMats.RData")

edgeShuffle <- function(el,tol=.001){
	iter = 0
	el[,2] <- sample(el[,2],length(el[,2]))
	dupel <- duplicated(el)
	dup <- mean(dupel)
	while(dup > tol){
		edup <- el[which(dupel),2]
		el[which(dupel),2] <- sample(edup,length(edup))
		dupel <- duplicated(el)
		dup <- mean(dupel)
		iter = iter + 1
		#print(iter)
	}
	unique(el)
}


corRes <- list()

ts <- (2:11)*2 -1
ind <- 1
for(t in ts){
amat <- dats[[t]]
edgeind <- which(amat >0,arr.ind=T)
el <- cbind(rownames(amat)[edgeind[,1]],colnames(amat)[edgeind[,2]])
el <- cbind(match(el[,1],rownames(amat)),match(el[,2],colnames(amat)))

truMat <- matrix(0,nrow(amat),ncol(amat))
truMat[el] <- 1

simEMat <- NULL

truCors <- cor(t(truMat))

comCors <- c(.1,.2,.3,.4,.5,.6,.7,.8,.9)
truExt <- numeric(length(comCors))
ytru <- c(truCors)
for(i in 1:length(comCors)){
ifelse(comCors[i] <0, truExt[i] <- mean(ytru < comCors[i],na.rm=T), truExt[i] <- mean(ytru > comCors[i],na.rm=T)) 
}

for(k in 1:5){

simel <- edgeShuffle(el,tol=.01)
simMat <- matrix(0,nrow(amat),ncol(amat))
simMat[simel] <- 1

simCors <- cor(t(simMat))

comCors <- c(.1,.2,.3,.4,.5,.6,.7,.8,.9)
simExt <- numeric(length(comCors))
ysim <- c(simCors) 
for(i in 1:length(comCors)){ 
ifelse(comCors[i] <0,simExt[i] <- mean(ysim < comCors[i],na.rm=T) , simExt[i] <- mean(ysim > comCors[i],na.rm=T) )
}

simEMat <- cbind(simEMat,simExt)

}

corRes[[ind]] <- list(truExt,simEMat)
print(ind)
ind <- ind + 1 

}

save(list="corRes",file="~/Dropbox/professional/Research/Submitted/Camp/Data/corRes.RData")

load("~/Dropbox/professional/Research/Submitted/Camp/Data/corRes.RData")


for(i in 2:9){
filet <- paste("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Tex/corplot",1990+i*2,".pdf",sep="")
pdf(filet,width=5,height=3,pointsize=14)
par(las=1,mar=c(4,4,1,1))
truExt <- corRes[[i]][[1]]
simEMat <- corRes[[i]][[2]]

ratioMat <- simEMat
for(i in 1:5){
	ratioMat[,i] <- truExt/simEMat[,i]
}

plot(comCors,ratioMat[,1],type="l",ylim=c(0,3.5),ylab="(% Obs >X)/(% Sim >X)",xlab="")

for(i in 2:5){
	lines(comCors,ratioMat[,i])
}
abline(h=1,col="red",lwd=2)
title(xlab="X",line=2.25)


dev.off()
}


filet <- paste("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Tex/corplot.pdf",sep="")
pdf(filet,width=6,height=3.5,pointsize=10)
par(las=1,mar=c(0.1,4.00,1,0.001),mfrow=c(4,2))
comCors <- c(.1,.2,.3,.4,.5,.6,.7,.8,.9)
for(i in 2:9){
yr <- 1990+i*2
truExt <- corRes[[i]][[1]]
simEMat <- corRes[[i]][[2]]

ratioMat <- simEMat
for(i in 1:5){
	ratioMat[,i] <- truExt/simEMat[,i]
}

plot(comCors,ratioMat[,1],type="l",ylim=c(0,3.5),ylab="%Obs/%Sim",xlab="",xaxt="n",xlim=c(.05,.95))
abline(v=comCors,lty=2,col="grey65")
for(i in 1:5){
	lines(comCors,ratioMat[,i])
}
abline(h=1,col="red",lwd=2)
#title(xlab="X",line=2.25)
text(comCors-.03,.5,comCors)
text(.85,3,yr)
}
dev.off()


### Plotting  PAC distributions ###
ts <- (4:11)*2 -1
filet <- paste("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Tex/PACDegreeAmt.pdf",sep="")
pdf(filet,width=6,height=5,pointsize=16)
par(las=1,mar=c(4,4,1,1))
i <- 5
candFile <- paste("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Data/DynamicClustering/ClusteringResultsDyn",1989+i,".csv",sep="")
candDat <- read.csv(candFile,stringsAsFactors=F)
incs <- candDat$CID[which(candDat$CRPICO=="I")]
amat <- dats[[i]]
amat <- amat[,is.element(colnames(amat),incs)]
y <- apply(amat,1,sum)
y <- y[y>0]
qtiles <- seq(0.001,1,length=100)
qty <- quantile(y,prob=qtiles)
plot(qty,qtiles,log="x",type="l",xaxt="n",xlim=c(100,10000000), col=grey((80-i*3.5)/100),lwd=1.5, ylab = "Empirical Cumulative Proportion",xlab="")
title(xlab="Total Amount Contributed by PAC",line=2.25)
abline(h=.5,lwd=2,col="red")
points <- 10^(1:7)
labs <- as.character(points)
axis(1,at=points,lab=labs)

for(i in ts){
candFile <- paste("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Data/DynamicClustering/ClusteringResultsDyn",1989+i,".csv",sep="")
candDat <- read.csv(candFile,stringsAsFactors=F)
incs <- candDat$CID[which(candDat$CRPICO=="I")]
amat <- dats[[i]]
amat <- amat[,is.element(colnames(amat),incs)]
y <- apply(amat,1,sum)	
y <- y[y>0]
qtiles <- seq(0.001,1,length=100)
qty <- quantile(y,prob=qtiles)
lines(qty,qtiles,type="l",xaxt="n",col=grey((80-i*3.5)/100),lwd=1.5)
}

for(i in c(5,ts)){
candFile <- paste("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Data/DynamicClustering/ClusteringResultsDyn",1989+i,".csv",sep="")
candDat <- read.csv(candFile,stringsAsFactors=F)
chals <- candDat$CID[which(candDat$CRPICO=="C")]
amat <- dats[[i]]
amat <- amat[,is.element(colnames(amat),chals)]
y <- apply(amat,1,sum)	
y <- y[y>0]
qtiles <- seq(0.001,1,length=100)
qty <- quantile(y,prob=qtiles)
lines(qty,qtiles,type="l",xaxt="n",col=grey((80-i*3.5)/100),lwd=1.5,lty=2)
}

dev.off()


ts <- (4:11)*2 -1
filet <- paste("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Tex/PACDegreeNum.pdf",sep="")
pdf(filet,width=6,heigh=5,pointsize=16)
par(las=1,mar=c(4,4,1,1))
i <- 5
candFile <- paste("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Data/DynamicClustering/ClusteringResultsDyn",1989+i,".csv",sep="")
candDat <- read.csv(candFile,stringsAsFactors=F)
incs <- candDat$CID[which(candDat$CRPICO=="I")]
amat <- dats[[i]]
amat <- amat[,is.element(colnames(amat),incs)]
y <- apply(amat>0,1,sum)
y <- y+.001
qtiles <- seq(0.001,.99,length=100)
qty <- quantile(y,prob=qtiles)
plot(qty,qtiles,type="l",xlim=c(0,250), col=grey((80-i*3.5)/100),lwd=1.5, ylab = "Empirical Cumulative Proportion",xlab="")
title(xlab="Total Candidates Supported by PAC",line=2.25)
abline(h=.5,lwd=2,col="red")
#points <- round(3.25^(1:5))
#labs <- as.character(points)
#axis(1,at=points,lab=labs)

for(i in ts){
candFile <- paste("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Data/DynamicClustering/ClusteringResultsDyn",1989+i,".csv",sep="")
candDat <- read.csv(candFile,stringsAsFactors=F)
incs <- candDat$CID[which(candDat$CRPICO=="I")]
amat <- dats[[i]]
amat <- amat[,is.element(colnames(amat),incs)]
y <- apply(amat>0,1,sum)	
y <- y+.001
qtiles <- seq(0.001,1,length=100)
qty <- quantile(y,prob=qtiles)
lines(qty,qtiles,type="l",xaxt="n",col=grey((80-i*3.5)/100),lwd=1.5)
}

for(i in c(5,ts)){
candFile <- paste("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Data/DynamicClustering/ClusteringResultsDyn",1989+i,".csv",sep="")
candDat <- read.csv(candFile,stringsAsFactors=F)
chals <- candDat$CID[which(candDat$CRPICO=="C")]
amat <- dats[[i]]
amat <- amat[,is.element(colnames(amat),chals)]
y <- apply(amat>0,1,sum)	
y <- y+.001
qtiles <- seq(0.001,1,length=100)
qty <- quantile(y,prob=qtiles)
lines(qty,qtiles,type="l",xaxt="n",col=grey((80-i*3.5)/100),lwd=1.5,lty=2)
}

dev.off()


#### Candidates ####
ts <- (4:11)*2 -1
filet <- paste("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Tex/CandDegreeAmt.pdf",sep="")
pdf(filet,width=6,heigh=5,pointsize=16)
par(las=1,mar=c(4,4,1,1))
i <- 5
candFile <- paste("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Data/DynamicClustering/ClusteringResultsDyn",1989+i,".csv",sep="")
candDat <- read.csv(candFile,stringsAsFactors=F)
incs <- candDat$CID[which(candDat$CRPICO=="I")]
amat <- dats[[i]]
amat <- amat[,is.element(colnames(amat),incs)]
y <- apply(t(amat),1,sum)
y <- y[y>=0]
qtiles <- seq(0.001,1,length=100)
qty <- quantile(y,prob=qtiles)
plot(qty,qtiles,log="x",type="l",xaxt="n",xlim=c(1,10000000), col=grey((80-i*3.5)/100),lwd=1.5, ylab = "Empirical Cumulative Proportion",xlab="")
title(xlab="Total Amount Contributed to Candidate",line=2.25)
abline(h=.5,lwd=2,col="red")
points <- c(1,10^(1:7))
labs <- as.character(c(0,points))
axis(1,at=c(.01,points),lab=labs)

for(i in ts){
candFile <- paste("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Data/DynamicClustering/ClusteringResultsDyn",1989+i,".csv",sep="")
candDat <- read.csv(candFile,stringsAsFactors=F)
incs <- candDat$CID[which(candDat$CRPICO=="I")]
amat <- dats[[i]]
amat <- amat[,is.element(colnames(amat),incs)]
y <- apply(t(amat),1,sum)	
y <- y[y>=0]
qtiles <- seq(0.001,1,length=100)
qty <- quantile(y,prob=qtiles)
lines(qty,qtiles,type="l",xaxt="n",col=grey((80-i*3.5)/100),lwd=1.5)
}

for(i in c(5,ts)){
candFile <- paste("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Data/DynamicClustering/ClusteringResultsDyn",1989+i,".csv",sep="")
candDat <- read.csv(candFile,stringsAsFactors=F)
chals <- candDat$CID[which(candDat$CRPICO=="C")]
amat <- dats[[i]]
amat <- amat[,is.element(colnames(amat),chals)]
y <- apply(t(amat),1,sum)	
y <- y[y>=0]
y=y+.01
qtiles <- seq(0.001,1,length=100)
qty <- quantile(y,prob=qtiles)
lines(qty,qtiles,type="l",xaxt="n",col=grey((80-i*3.5)/100),lwd=1.5,lty=2)
}

dev.off()


ts <- (4:11)*2 -1
filet <- paste("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Tex/CandDegreeNum.pdf",sep="")
pdf(filet,width=6,heigh=5,pointsize=16)
par(las=1,mar=c(4,4,1,1))
i <- 5
candFile <- paste("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Data/DynamicClustering/ClusteringResultsDyn",1989+i,".csv",sep="")
candDat <- read.csv(candFile,stringsAsFactors=F)
incs <- candDat$CID[which(candDat$CRPICO=="I")]
amat <- dats[[i]]
amat <- amat[,is.element(colnames(amat),incs)]
y <- apply(t(amat)>0,1,sum)
y <- y+.001
qtiles <- seq(0.001,.99,length=100)
qty <- quantile(y,prob=qtiles)
plot(qty,qtiles,type="l",xlim=c(0,600), col=grey((80-i*3.5)/100),lwd=1.5, ylab = "Empirical Cumulative Proportion",xlab="")
title(xlab="Total PACs Supporting Candidate",line=2.25)
abline(h=.5,lwd=2,col="red")
#points <- round(3.25^(1:5))
#labs <- as.character(points)
#axis(1,at=points,lab=labs)

for(i in ts){
candFile <- paste("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Data/DynamicClustering/ClusteringResultsDyn",1989+i,".csv",sep="")
candDat <- read.csv(candFile,stringsAsFactors=F)
incs <- candDat$CID[which(candDat$CRPICO=="I")]
amat <- dats[[i]]
amat <- amat[,is.element(colnames(amat),incs)]
y <- apply(amat>0,2,sum)	
y <- y+.001
qtiles <- seq(0.001,1,length=100)
qty <- quantile(y,prob=qtiles)
lines(qty,qtiles,type="l",xaxt="n",col=grey((80-i*3.5)/100),lwd=1.5)
}

for(i in c(5,ts)){
candFile <- paste("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Data/DynamicClustering/ClusteringResultsDyn",1989+i,".csv",sep="")
candDat <- read.csv(candFile,stringsAsFactors=F)
chals <- candDat$CID[which(candDat$CRPICO=="C")]
amat <- dats[[i]]
amat <- amat[,is.element(colnames(amat),chals)]
y <- apply(amat>0,2,sum)	
y <- y+.001
qtiles <- seq(0.001,1,length=100)
qty <- quantile(y,prob=qtiles)
lines(qty,qtiles,type="l",xaxt="n",col=grey((80-i*3.5)/100),lwd=1.5,lty=2)
}

dev.off()

